//
//  ArticleClassificationVC.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/18.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "ArticleClassificationVC.h"
#import "PlatformFunctionCell.h"
#import "ArticleClassifyModel.h"
#import "ArticleClassifyAddViewController.h"
#import "MenuView.h"

@interface ArticleClassificationVC ()<UITableViewDataSource, UITableViewDelegate> {
    UITableView *_tableView;
    NSMutableArray *_cellArr;
    
    NSMutableArray *aClassifyList;
}

@property (nonatomic, retain) OnePlatformModel *model;
@property (nonatomic, retain) MenuView * menuView;

@end

@implementation ArticleClassificationVC

- (id)initWithPlatformModel:(OnePlatformModel *)model {
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"文章分类";
    [self rightMenuBtn];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    
    [self.view addSubview:_tableView];
    
    aClassifyList = [NSMutableArray array];
    
    [self loadArticleClassList];
    
    
}

#pragma mark -- 加载数据 --
- (void)loadArticleClassList {
    self.loading = YES;
    NSDictionary *dic = @{@"p_account":self.model.account};
    DLog(@"%@",self.model.account);
    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/index/ArticleClassifyList.html" parameters:dic finish:^(BOOL seccess, NSString *error, id obj) {
        
        self.loading = NO;
        if (seccess) {
            NSNumber *code = obj[@"state"][@"code"];
            if (code.intValue != 0) {
                [self showText:obj[@"msg"]];
                return ;
            }
            NSMutableArray *marray = [NSMutableArray array];
            NSArray *list = obj[@"list"];
            for (NSDictionary *dic in list) {
                ArticleClassifyModel *model = [[ArticleClassifyModel alloc] initWithDictionary:dic];
                [marray addObject:model];
            }
            aClassifyList = marray;
            [_tableView reloadData];
        }
        
    }];
}
#pragma mark -
#pragma mark -- 菜单按钮点击事件 --
- (void)menuAction {
    NSArray * arr = @[@"添加分类", @"删除分类"];
    _menuView = [[MenuView alloc] initWithButtonTitles:arr withDelegate:self];
    _menuView.hasImage = NO;
    [_menuView showInView:self.view origin:CGPointMake(self.view.width - 180, 70)];
    _menuView.buttonView.delegate = self;
}

- (void)rightMenuBtn {
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menuAction)];
}

#pragma mark -
#pragma mark -- tableViewDelegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return aClassifyList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    ArticleClassifyModel *model = aClassifyList[indexPath.row];
    cell.textLabel.text = model.aClassifyName;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView!=_tableView) {
        return 40;
    }else{
        return 60.f;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView != _tableView) {
        UIViewController *viewController;
        [_menuView hide];
        switch (indexPath.row) {
            case 0: // 添加
                [self pushViewController:[[ArticleClassifyAddViewController alloc] initWithPlatformModel:self.model]];
                break;
            case 1: // 删除
                [_tableView setEditing:YES];
                [self setRightBarButton:@"完成" selector:@selector(rightMenuBtn)];
                break;
                
            default:
                break;
        }
        [self pushViewController:viewController];
    } else {
        // 编辑文章分类
        ArticleClassifyModel *model = aClassifyList[indexPath.row];
        UIViewController *viewController = [[ArticleClassifyAddViewController alloc] initWithPlatformModel:self.model acModel:model];
        [self pushViewController:viewController];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        self.loading = YES;
        ArticleClassifyModel *acmodel = aClassifyList[indexPath.row];
        NSDictionary *dic = @{@"p_account":self.model.account, @"a_classify_id":[NSString stringWithFormat:@"%lu",(unsigned long)acmodel.aClassifyId]};
        [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/index/DelArticleClassify.html" parameters:dic finish:^(BOOL seccess, NSString *error, id obj) {
            self.loading = NO;
            if (seccess) {
                NSNumber *code = obj[@"code"];
                if (code.intValue != 0) {
                    [self showText:obj[@"msg"]];
                } else {
                    [aClassifyList removeObjectAtIndex:indexPath.row];
                    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    [self showText:@"删除成功"];
                }
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
